import time

from selenium import webdriver
from selenium.webdriver.common.by import By
import allure
from wordpress_project.base.logger import Logger
import os

# 拿到父级的父级
file = os.path.dirname(os.path.dirname(__file__))
log_path = f'{file}/logs/wordpress.log'  # 日志存放路径


class Tools:
    def __init__(self, driver):
        # driver:浏览器，只要你传递进来的浏览器操作
        self.log = Logger(file_name=log_path)
        self.driver = driver
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def visit(self, url):
        # 访问网页 url:网页地址
        self.log(f'正在访问{url}该网页')
        self.driver.get(url)
        time.sleep(0.5)

    def by_css(self, css, index=0):
        return self.driver.find_elements(By.CSS_SELECTOR, css)[index]

    def by_link(self, link):
        return self.driver.find_element(By.PARTIAL_LINK_TEXT, link)

    def quit(self):
        self.driver.quit()

    def img(self):
        title = time.strftime('%Y_%m_%d_%H_%M_%S')
        # 截图、附加在测试报告中
        allure.attach(self.driver.get_screenshot_as_png(), title, allure.attachment_type.PNG)


# d = webdriver.Chrome()
# d.get("https://www.baidu.com")

